//
//  UIView+MY.m
//  JQLive
//
//  Created by Bruce on 2020/3/3.
//  Copyright © 2020 mlqk. All rights reserved.
//

#import "UIView+MY.h"
#import <objc/runtime.h>

const char *blockKey = "blockKey";

@implementation UIView (MY)

- (void)setTapBlock:(TapBlock)tapBlock {
    objc_setAssociatedObject(self, &blockKey, tapBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (TapBlock)tapBlock {
    return objc_getAssociatedObject(self, &blockKey);
}

- (void)my_addTapGes:(TapBlock)block {
    self.tapBlock = block; // 把block赋值给self.tapBlock, 可以在当前函数之外执行该回调
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes)];
    [self addGestureRecognizer:tap];
}

- (void)tapGes {
    if (self.tapBlock) {
        self.tapBlock(); // 执行回调
    }
}

#pragma mark 获取最上面的控制器
- (UIViewController*)currentViewController {
    UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (1) {
        
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else{
            break;
        }
    }
    return vc;
}

- (void)moveToHorizontal:(CGFloat)horizontal toVertical:(CGFloat)vertical
{
    CGRect origionRect = self.frame;
    CGRect newRect = CGRectMake(horizontal, vertical, origionRect.size.width, origionRect.size.height);
    self.frame = newRect;
}

- (void)addBouncesAnimation
{
    CAKeyframeAnimation *frameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    frameAnimation.duration = 0.35;
    frameAnimation.values = @[
                              [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)],
                              [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)],
                              [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)],
                              [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]
                              ];
    [self.layer addAnimation:frameAnimation forKey:nil];
}

- (CGFloat)x
{
    return CGRectGetMinX(self.frame);
}

- (CGFloat)y
{
    return CGRectGetMinY(self.frame);
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setSize:(CGSize)size {
    CGRect newFrame = self.frame;
    newFrame.size = size;
    self.frame = newFrame;
}

- (CGFloat)width
{
    return CGRectGetWidth(self.frame);
}

- (CGFloat)height
{
    return CGRectGetHeight(self.frame);
}

- (CGFloat)boundsX
{
    return CGRectGetMinX(self.bounds);
}

- (CGFloat)boundsY
{
    return CGRectGetMinY(self.bounds);
}

- (CGFloat)boundsW
{
    return CGRectGetWidth(self.bounds);
}

- (CGFloat)boundsH
{
    return CGRectGetHeight(self.bounds);
}

@end
